/*
 * =====================================================================================
 *
 *       Filename:  test_timer_service.cxx
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  02/28/2011 07:49:57 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ken Chen (Ken), chen_kenz@emc.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#include <iostream>
#include <cstring>
#include "cl_timestamp.hxx"
#include "cl_timer.hxx"
#include "cl_timer_service.hxx"
#include "cl_thread.hxx"
#include "cl_singleton.hxx"
#include "cl_event_looper.hxx"
#include "cl_signal_handler.hxx"

using namespace std;
using namespace neo;

void Print1(void)
{
  static CLTimestamp history = CLTimestamp::nowTime();
  CLTimestamp now = CLTimestamp::nowTime();
  cout << "Print1 history: " << history << "\n";
  cout << "Print1 now: " << now << "\n";
	history = now;
}

void Print2(void)
{
  static CLTimestamp history = CLTimestamp::nowTime();
  CLTimestamp now = CLTimestamp::nowTime();
  cout << "Print2 history: " << history << "\n";
  cout << "Print2 now: " << now << "\n";
	history = now;
}

void Print3(void)
{
  static CLTimestamp history = CLTimestamp::nowTime();
  CLTimestamp now = CLTimestamp::nowTime();
  cout << "Print3 history: " << history << "\n";
  cout << "Print3 now: " << now << "\n";
	history = now;
}

void print4(void)
{
	cout << "Print4 run at: " << CLTimestamp::nowTime().toFormatString() << "\n";
}

void print5(void)
{
	cout << "Print5 run at: " << CLTimestamp::nowTime().toFormatString() << "\n";
}

int main(int argc, char* argv[])
{
  CLEventLooper looper;
	CLSignalHandler sigHandler;

	CLId timer1 = looper.runJobEvery(1000000, bind(Print1));
	CLId timer2 = looper.runJobEvery(3000000, bind(Print2));
	CLId timer3 = looper.runJobEvery(5000000, bind(Print3));

  looper.start();

	sleep(10);
	looper.cancelTimerJob(timer1);
	looper.cancelTimerJob(timer2);
	looper.cancelTimerJob(timer3);

	looper.runJobAt(CLTimestamp::nowTime() + 1000000, bind(print4));
	looper.runJobAfter(10000000, bind(print5));

	sigHandler.waitForSignal();
}

